<?php
class StationLetterUser extends Eloquent {

    protected $fillable = array('to', 'user_id');

    public static function addLetter($to_id, $creator_id)
    {
        if ($creator_id <= 0)
        {
            return;
        }
        $stationLetterUser = StationLetterUser::firstOrCreate(array(
            'to' => $to_id,
            'user_id' => $creator_id
        ));

        $stationLetterUser->increment('unread_count', 1);
        $stationLetterUser->increment('total_count', 1);
        $stationLetterUser->touch();
        $stationLetterUser->save();
    }

    public static function readLetter($to_id, $creator_id)
    {
        $stationLetterUser = StationLetterUser::where('to', '=', $to_id)->where('user_id', '=', $creator_id)->first();
        if (! $stationLetterUser)
        {
            throw new Exception(sprintf("Can not find station letter user record %s, %s", $to_id, $creator_id));
        }

        $stationLetterUser->decrement('unread_count', 1);
        $stationLetterUser->touch();
        $stationLetterUser->save();
    }

    public static function getLetterUserList()
    {

        $station_letter_users = StationLetterUser::where('to', '=', Auth::user()->id)->orderBy('updated_at', 'DESC')->get();
        $i = 0;
        foreach($station_letter_users as $key){    	
        	$station_letter_users[$i]->realname = '来自'.$station_letter_users[$i]->creatorName();
			//zhb $station_letter_users[$i]->message='标题';
        	$station_letter_users[$i]->avatar = $station_letter_users[$i]->creatorAvatar();
        	$i++;
        }
        return $station_letter_users;
    }

	public static function getUnreadCount()
	{
        $user = Auth::user();
        if (! $user)
        {
            return;
        }
       $u_letter_cnt =  StationLetterUser::where('to', '=', $user->id)->orderBy('updated_at', 'DESC')->sum('unread_count');
       $s_letter_cnt =  StationLetter::where('to', '=', $user->id)->where('creator_id', '=', 0)->where('is_read', '=', 0)->orderBy('updated_at', 'DESC')->count();
	   return $u_letter_cnt+$s_letter_cnt;
	}

    public function creatorName()
    {
        $user = User::find($this->user_id);
        $customer = Customer::where('user_id', $this->user_id)->first();
        if ($customer)
        {
            return $customer->realname ? $customer->realname : $user->email;
        }
        $company = Company::where('user_id', $this->user_id)->first();
        if ($company)
        {
            return $company->name ? $company->name : $user->email;
        }
        $lawyer = Lawyer::where('user_id', $this->user_id)->first();
        if ($lawyer)
        {
            return $lawyer->realname ? $lawyer->realname.'律师' : $user->email;
        }

        return $user->email;
    }

    public function creatorAvatar()
    {
        return $this->user_id ? User::find($this->user_id)->avatar : '';
    }

    public function creator()
    {
        $customer = Customer::where('user_id', $this->user_id)->first();
        if ($customer)
        {
            return $customer;
        }

        $company = Company::where('user_id', $this->user_id)->first();
        if ($company)
        {
            return $company;
        }

        $lawyer = Lawyer::where('user_id', $this->user_id)->first();
        if ($lawyer)
        {
            return $lawyer;
        }

        return User::find($this->user_id);
    }

}